﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;


namespace dkGameTool
{
    public partial class addGkfrm : UserControl
    {
        public addGkfrm(string deviceId)
        {
            this.DeviceID = deviceId;
            InitializeComponent();
        }

        string DeviceID;

        string code;
        string _path;
        string _name;
        decimal _number;

        public string Code { get => "tz_" + code; set => code = value; }
        public string ImgPath { get => "./img/" + _path; set => _path = value; }
        public string GkName { get => _name; set => _name = value; }
        public decimal GkNumber { get => _number; set => _number = value; }

        private void btnAddGk_Click(object sender, EventArgs e)
        {
            Code = this.txtCode.Text;
            GkName = this.txtName.Text;
            GkNumber = this.nudGkNumber.Value;



            if (string.Empty == code)
            {
                MessageBox.Show("编码不能为空");
                return;
            }

            Boolean exits = DataBaseManager.GetInstance.getCodeIsExist(Code);
            if (exits)
            {
                MessageBox.Show("编码重复请重新填写编码");
                return;
            }

            if (string.Empty == GkName)
            {
                MessageBox.Show("关卡名称不能为空");
                return;
            }

            if (string.Empty == _path)
            {
                MessageBox.Show("未选择图片");
                return;
            }

            string sql = string.Format("insert into img_res (code, path, name) values ('{0}','{1}','{2}');", Code, ImgPath, Code);
            DataBaseManager.GetInstance.ExcuterSql(sql);
            string sql1 = string.Format("insert into  challenge ( chapter, section, name, path, code) values ({3},1,'{0}','{1}','{2}');", GkName, ImgPath, Code, GkNumber);
            DataBaseManager.GetInstance.ExcuterSql(sql1);

            MessageBox.Show("添加成功");
        }

        private void button1_Click(object sender, EventArgs e)
        {
            openFileDialog1.InitialDirectory = ".\\img";
            openFileDialog1.FilterIndex = 2;

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                ImgPath = openFileDialog1.FileName;

                ImgPath = Path.GetFileName(ImgPath);
                txtPath.Text = ImgPath;
            }
        }
    }
}
